﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using INdTFramework.Persistence.Storage;

namespace INdTFramework.Http.OAuth
{
    public class FacebookAuth
    {
        private readonly AppSettingsStorage iso = new AppSettingsStorage();
        private const string LoginRequest = "https://m.facebook.com/dialog/oauth?locale2=pt_BR&client_id={0}&redirect_uri=https://www.facebook.com/connect/login_success.html&scope={1}&display=touch";
        private const string TokenRequest = "https://graph.facebook.com/oauth/access_token?client_id={0}&redirect_uri=https://www.facebook.com/connect/login_success.html&client_secret={1}&code={2}";
        private const string UserRequest = "https://graph.facebook.com/me?fields=name,picture&access_token={0}";

        public string AccessToken { get; set; }
        public string AppId { get; set; }
        public string Secret { get; set; }
        public string Scope { get; set; }

        public bool KeepUserLogged { get; set; }

        /// <summary>
        /// Loads the previous saved data.
        /// </summary>
        public FacebookAuth(String appId, String secret, String scope)
        {
            AppId = appId;
            Secret = secret;
            Scope = scope;
            //FacebookAuth auth = iso.Get<FacebookAuth>("-fb-auth-data");

            //if (auth != null)
            //{
            //    AppId = auth.AppId;
            //    Secret = auth.Secret;
            //    Scope = auth.Scope;
            //    KeepUserLogged = auth.KeepUserLogged;
            //}
        }

        public String GetLoginUrl()
        {
            return String.Format(LoginRequest, AppId, Scope);
        }

        public String GetAccessTokenRequestUrl(string code)
        {
            return String.Format(TokenRequest, AppId, Secret, code);
        }

        public String GetIdUser(string accessToken)
        {
            return String.Format(UserRequest, accessToken);
        }
    }
}
